package math.probability;

import java.util.ArrayList;
import java.util.Random;

public class LaplasDistribution extends ProbDistribution {
	private Double alpha;
	private Double betta;
	
	public LaplasDistribution(Double a, Double b) {
		this.alpha = a;
		this.betta = b;
	}
	
	public LaplasDistribution() {
		this.alpha = 1.0;
		this.betta = 0.0;
	}
	
	
	public ArrayList<Double> getSample(int n) {
		ArrayList<Double> sample = new ArrayList<Double>();
		for (int i = 0; i < n; i++) {
			sample.add(getNext());
		}
		return sample;
	}
	
	public ArrayList<Double> getSample(int n, Random r) {
		ArrayList<Double> sample = new ArrayList<Double>();
		for (int i = 0; i < n; i++) {
			sample.add(getNext(r));
		}
		return sample;
	}
	
	public String toString() {
		return "L" + String.format("(%2.2f;%2.2f)", alpha, betta);
		 
	}

	@Override
	public Double getNext() {
		Random r = new Random();
		Double v;
		Double rnd = r.nextDouble();
		if (rnd < 0.5)
			v = Math.log(2*rnd)/alpha + betta;
		else
			v = -Math.log(2*(1-rnd))/alpha + betta;
		return v;
	}
	
	public Double getNext(Random r) {
		Double v;
		Double rnd = r.nextDouble();
		if (rnd < 0.5)
			v = Math.log(2*rnd)/alpha + betta;
		else
			v = -Math.log(2*(1-rnd))/alpha + betta;
		return v;
	}
	
	public Double getDisp() {
		return 2/Math.pow(alpha, 2.0);
	}
}
